package com.bigknow.appstore.proxy.handler.clear;

import com.bigknow.appstore.proxy.cache.ICacheManager;
import com.bigknow.appstore.proxy.handler.base.BaseOutboundHandler;
import com.bigknow.appstore.proxy.log.ILogWriter;
import com.bigknow.appstore.proxy.server.IProxyServer;
import com.bigknow.appstore.proxy.util.HandlerVariables;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http.FullHttpMessage;

/**
 * 清空每次为response独立保存的requestVo信息，也就是说这个handler必须放在所有逻辑处理handler的最后
 * Created by chaos on 2016/9/21.
 */
@ChannelHandler.Sharable
public class ClearResponseRequestMapInfoHandler extends BaseOutboundHandler {

    public ClearResponseRequestMapInfoHandler(IProxyServer proxyServer, ICacheManager cacheManager, ILogWriter writer) {
        super(proxyServer, cacheManager, writer);
    }

    @Override
    public void write(ChannelHandlerContext ctx, FullHttpMessage msg, ChannelPromise promise) {
        if (HandlerVariables.requestVoForCurrentResponse.containsKey(ctx.channel().id().asLongText()))
            HandlerVariables.requestVoForCurrentResponse.remove(ctx.channel().id().asLongText());
        ctx.write(msg, promise);
    }
}
